<?php
/**
 * ClassName: reservation
 * Description: vip会员预约
 * @Author 周昌
 * @Create 2023/11/24 16:18
 */

namespace app;


use epii\server\Args;
use libs\Tool;
use model\ReservationModel;
use service\admin\HospitalService;
use service\admin\MemberService;
use service\admin\ReservationService;
use service\admin\RoomService;
use think\Db;

class reservation extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();

    }

    public function index()
    {
        $type=Args::params('type/d');
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        //筛选
        $this->_as_status = ReservationService::reservationStatusSelect();
        $this->assign('type',$type);
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //登录用户权限判断
        $hospital_id=$this->adminInfo['hospital_id'];
        if($hospital_id){
            $map[]=['hospital_id','=',$hospital_id];
        }
        $type = Args::params('type');
        if (is_numeric($type)&&$type>=0) {
            $map[] = ['type', "=", $type];
        }
        //预约状态
        $status = Args::params('status/d');
        if (is_int($status)) {
            $map[] = ['status', "=", $status];
        }
        $name = Args::params('name');
        if ($name) {
            $map[] = ['name', "LIKE", '%' . $name . '%'];
        }
        $phone = Args::params('phone');
        if ($phone) {
            $map[] = ['phone', "=", $phone];
        }
        $hospital_id = Args::params('hospital_id');
        if ($hospital_id) {
            $map[] = ['hospital_id', "=", $hospital_id];
        }
        //自动更新时间过期的未签订预约为已过期状态
        ReservationService::autoCancel();
        echo ReservationService::getAjaxData($map);
    }

    //编辑
    public function edit()
    {
        $id = Args::params('id/d');
        if(Args::isPost()&&$id){
            //只修改状态
            $data=Args::params('r');
            $this->validate($data,[
                'status'=>'integer|>=:0'
            ]);
            $res=ReservationService::edit($id,['status'=>$data['status']]);
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $info=ReservationModel::getInfo(['id'=>$id]);
        $this->_as_info = $info;
        $this->_as_id = $id;
        $this->_as_status = ReservationService::reservationStatusSelect();
        $this->_as_sex = MemberService::memberSexSelect();
        $this->_as_hospitals = HospitalService::getSelect(['id'=>$info['hospital_id']]);

        $this->adminUiDisplay();
    }

    /**
     * @return array|false|string
     * @throws \think\Exception
     * @throws \think\db\exception\PDOException
     * 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断该事项下是否已经有床位，无则可以删除
        $res = ReservationService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }
    //分配床位
    public function bed()
    {
        $id=Args::params('id/d');
        if(Args::isPost()){
            $data=Args::params('r');
            $this->validate($data,[
                'room_id|房间'=>'require|integer|>:0',
                'bed_id|床位'=>'require|integer|>:0',
            ]);
            $res=ReservationService::allocateBed($id,$data['room_id'],$data['bed_id']);
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $info=ReservationModel::getInfo(['id'=>$id]);
        $this->_as_info = $info;
        $this->_as_id = $id;
        $room_where=[
            'hospital_id'=>$this->adminInfo['hospital_id'],
            'status'=>RoomService::$status_index['yes'],
            'is_vip'=>RoomService::$is_vip_index['yes']
        ];
        $this->_as_room_select=RoomService::getSelect($room_where);

        $this->adminUiDisplay();
    }


}